package com.kj.mynote.Util;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.kj.mynote.Bean.NoteBean;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

/**
 * @author wy
 * @version V1.0
 * @Title: TimeNote
 * @Package com.kj.mynote.Util
 * @Description: TODO(用一句话描述该文件做什么)
 * @date 17/3/28 18:39
 */

public class ExcelUtils {
    public static WritableFont arial14font = null;

    public static WritableCellFormat arial14format = null;
    public static WritableFont arial10font = null;
    public static WritableCellFormat arial10format = null;
    public static WritableFont arial12font = null;
    public static WritableCellFormat arial12format = null;

    public final static String UTF8_ENCODING = "UTF-8";
    public final static String GBK_ENCODING = "GBK";
    private static String[] title = {"ID", "所属分类", "概述", "记录开始时间", "记录结束时间", "总共用时"};


    public static WritableCellFormat initTitleFormat() {
        WritableCellFormat cellFormat = null;
        try {
            //设置字体;
            WritableFont font1 = new WritableFont(WritableFont.ARIAL, 14, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.RED);
            cellFormat = new WritableCellFormat(font1);
            //设置背景颜色;
            cellFormat.setBackground(Colour.BLUE);
            //设置边框;
            cellFormat.setBorder(Border.ALL, BorderLineStyle.DASH_DOT);
            //设置自动换行;
            cellFormat.setWrap(true);
            //设置文字居中对齐方式;
            cellFormat.setAlignment(Alignment.CENTRE);
            //设置垂直居中;
            cellFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
        } catch (WriteException e) {
            e.printStackTrace();
        }
        return cellFormat;
    }

    public static WritableCellFormat initContentFormat() {
        WritableCellFormat cellFormat = null;
        try {
            //设置字体;
            WritableFont font1 = new WritableFont(WritableFont.ARIAL, 12, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
            cellFormat = new WritableCellFormat(font1);
            //设置边框;
            cellFormat.setBorder(Border.ALL, BorderLineStyle.DASH_DOT);
            //设置自动换行;
            cellFormat.setWrap(true);
            //设置文字居中对齐方式;
            cellFormat.setAlignment(Alignment.CENTRE);
            //设置垂直居中;
            cellFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
        } catch (WriteException e) {
            e.printStackTrace();
        }
        return cellFormat;
    }

    public static boolean writeObjListToExcel(List<NoteBean> list,
                                              String fileName, Context c) {
        boolean flag = false;
        WritableWorkbook workbook = null;
        try {
            File dir = new File(FileUtils.rootExcelDir);
            if(!dir.exists()){
                dir.mkdirs();
            }
            //创建Excel工作簿;
            workbook = Workbook.createWorkbook(new File(dir,fileName + ".xls"));
            //创建Excel电子薄;
            WritableSheet sheet = workbook.createSheet("便签导出", 0);
            sheet.setColumnView(0, 10);
            sheet.setColumnView(1, 20);
            sheet.setColumnView(2, 30);
            sheet.setColumnView(3, 25);
            sheet.setColumnView(4, 25);
            sheet.setColumnView(5, 20);
            //创建表头
            for (int i = 0; i < title.length; i++) {
                Label label = new Label(i, 0, title[i], initTitleFormat());
                sheet.setRowView(i, 800);
                sheet.addCell(label);
            }
            //写入内容
            for (int i = 0; i < list.size(); i++) {
                sheet.setRowView(i + 1, 400);
                NoteBean bean = list.get(i);
                sheet.addCell(new Label(0, i + 1, bean.getId() + "", initContentFormat()));
                sheet.addCell(new Label(1, i + 1, bean.getNoteClass(), initContentFormat()));
                sheet.addCell(new Label(2, i + 1, bean.getTitle(), initContentFormat()));
                sheet.addCell(new Label(3, i + 1, ToolUtils.TS2S(bean.getStartTime(), "yyyy-MM-dd HH:mm"), initContentFormat()));
                sheet.addCell(new Label(4, i + 1, ToolUtils.TS2S(bean.getEndTime(), "yyyy-MM-dd HH:mm"), initContentFormat()));
                sheet.addCell(new Label(5, i + 1, ToolUtils.TS2TS(bean.getStartTime(), bean.getEndTime()), initContentFormat()));
            }
            //写入Excel表格中;
            workbook.write();
            flag = true;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (WriteException e) {
            e.printStackTrace();
        } finally {
            try {
                workbook.close();
            } catch (WriteException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return flag;
    }

    public static List<NoteBean> read2DB(File f, Context con) {
        ArrayList<NoteBean> billList = new ArrayList<NoteBean>();
        try {
            Workbook course = null;
            course = Workbook.getWorkbook(f);
            Sheet sheet = course.getSheet(0);

            Cell cell = null;
            for (int i = 1; i < sheet.getRows(); i++) {
                NoteBean tc = new NoteBean();
                billList.add(tc);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return billList;
    }

    public static Object getValueByRef(Class cls, String fieldName) {
        Object value = null;
        fieldName = fieldName.replaceFirst(fieldName.substring(0, 1), fieldName
                .substring(0, 1).toUpperCase());
        String getMethodName = "get" + fieldName;
        try {
            Method method = cls.getMethod(getMethodName);
            value = method.invoke(cls);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return value;
    }
}
